<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: ScrollView With Pagination</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: ScrollView With Pagination</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
    <p>This example shows how to create a ScrollView widget with pagination support, using the ScrollViewPaginator plugin.</p>
</div>

<div class="example newwindow">
    <a href="scrollview-paging-example.html" target="_blank" class="button">
        View Example in New Window
    </a>
</div>

<h2>ScrollView with Pagination Support</h2>

<p>In this example, we'll create a ScrollView instance which has pagination support enabled. This allows the ScrollView to scroll between discrete page boundaries in the content, as opposed to continuous scrolling. Pagination is only supported for horizontal ScrollViews currently.</p>

<h3>Modules Used</h3>

<p>For this example, since we want both pagination and scrollbar indicator support enabled, we use the <code>scrollview</code> module as we did for the <a href="scrollview-horiz.html">Horizontal ScrollView</a> example to get the base ScrollView with scrollbar support.</p>

<p>Additionally we pull down the <code>scrollview-paginator</code> module, which provides the <code>ScrollViewPaginator</code> plugin:</p>

<pre class="code prettyprint">&#x2F;&#x2F; Pull in the scrollview widget, and the paginator plugin

YUI().use(&#x27;scrollview&#x27;, &#x27;scrollview-paginator&#x27;, function(Y) {
    ...
});</pre>


<h3>Instantiating The ScrollView Widget</h3>

<p>As with the <a href="scrollview-horiz.html">Horizontal ScrollView</a> example, we provide the markup for the ScrollView content on the page, as shown below:</p>

<pre class="code prettyprint">&lt;!-- Content with a width which would require scrolling --&gt;
&lt;div id=&quot;scrollview-content&quot; style=&quot;&quot; class=&quot;yui3-scrollview-loading&quot;&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;img src=&quot;...&quot;&gt;&lt;&#x2F;li&gt;
        &lt;li&gt;&lt;img src=&quot;...&quot;&gt;&lt;&#x2F;li&gt;
        &lt;li&gt;&lt;img src=&quot;...&quot;&gt;&lt;&#x2F;li&gt;
        &lt;li&gt;&lt;img src=&quot;...&quot;&gt;&lt;&#x2F;li&gt;
    &lt;&#x2F;ul&gt;
&lt;&#x2F;div&gt;</pre>


<p>And we instantiate the ScrollView instance in the same way, by providing a fixed width for the widget, and constraining flicks to the "x" axis:</p> 

<pre class="code prettyprint">&#x2F;&#x2F; Constraining the width, instead of the height for horizontal scrolling
var scrollView = new Y.ScrollView({
    id: &#x27;#scrollview&#x27;,
    srcNode: &#x27;#scrollview-content&#x27;,
    width : 320,
    flick: {
        minDistance:10,
        minVelocity:0.3,
        axis: &quot;x&quot;
    }
});</pre>


<p>As we did in the Horizontal ScrollView example, we add CSS which switches the LIs to layout horizontally:</p>

<pre class="code prettyprint">&#x2F;* To layout horizontal LIs *&#x2F;
#scrollview-content {
    white-space:nowrap;
}

#scrollview-content li {
    display:inline-block;
    background-color:#fff;
}

&#x2F;* For IE 6&#x2F;7 - needs inline block hack (and the background color mentioned above) *&#x2F;
#scrollview-content li {
    *display:inline;
    *zoom:1;
}</pre>


<p>This gives us a ScrollView instance with scrollbar indicator support. However it doesn't have pagination support available yet.</p>

<h3>Plugging In Pagination Support</h3>

<p>To add pagination support to the ScrollView instance, we plug in the <code>ScrollViewPaginator</code> plugin, providing the <code>selector</code> attribute configuration argument to it. The <code>selector</code> tells
the paginator which list of elements inside the ScrollView content box define page boundaries at which the ScrollView should stop when scrolling. For this example, each LI defines a ScrollView page:</p>

<pre class="code prettyprint">scrollView.plug(Y.Plugin.ScrollViewPaginator, {
    selector: &#x27;li&#x27;
});</pre>


<p>The ScrollView now has pagination support activated, and will stop at page boundaries when the user flicks the content, or drags the content past the halfway point of the current page.</p>

<h3>Accessing The Paginator Plugin API</h3>

<p>Similar to the ScrollBar indicator plugin, the ScrollBarPaginator API is now available on the ScrollView instance, on the namespace defined by the plugin, which is <code>scrollView.pages</code>. 
The <code>pages</code> property can be used to manage the page state of the ScrollView, as shown below:</p>

<pre class="code prettyprint">Y.one(&#x27;#scrollview-next&#x27;).on(&#x27;click&#x27;, Y.bind(scrollView.pages.next, scrollView.pages));
Y.one(&#x27;#scrollview-prev&#x27;).on(&#x27;click&#x27;, Y.bind(scrollView.pages.prev, scrollView.pages));</pre>


<p>The above code calls the plugin's <code>next()</code> and <code>prev()</code> methods when the respective button is clicked. The <a href="index.html#paginator">ScrollView Paginator</a> documentation provides additional examples of the API available through the <code>pages</code> property.</p>

<h3>Setting Up A Click Listener On the Content</h3>

<p>For this example, we also set up a click listener on the images. For touch devices, the click event does not fire when dragging or flicking, so there's no special handling required when setting up a click listener. However to also support mouse based devices, we need to distinguish between a click and drag or flick. In order to do this we set up a check in our click listener, to make sure we only respond to the click if the ScrollView wasn't dragged, by checking the <code>lastScrolledAmt</code> property, which is reset whenever a drag/flick gesture ends:</p>

<pre class="code prettyprint">Y.one(&quot;#scrollview-content&quot;).delegate(&quot;click&quot;, function(e) {
    &#x2F;&#x2F; For mouse based devices, we need to make sure the click isn&#x27;t fired
    &#x2F;&#x2F; and the end of a drag&#x2F;scroll. We use 2 as an arbitrary threshold.
    if (Math.abs(scrollView.lastScrolledAmt) &lt; 2) {
        alert(e.currentTarget.getAttribute(&quot;alt&quot;));
    }
}, &quot;img&quot;);</pre>


<h3>Preventing Native Behavior For Content</h3>

<p>As with the Horizontal ScrollView example, since we have images which act as drag/flick targets, we need to stop the default image drag/drop behavior in certain browsers (for example gecko and IE), by preventing default on the underlying mousedown event. If we don't prevent default, the image will be natively draggable by default, and interfere with scrolling:</p>

<pre class="code prettyprint">&#x2F;&#x2F; Prevent the image from being natively dragged
content.delegate(&quot;mousedown&quot;, function(e) {
    e.preventDefault();
}, &quot;img&quot;);</pre>


<h2>Complete Example Source</h2>
<pre class="code prettyprint">&lt;div id=&quot;scrollview-container&quot;&gt;
    &lt;div id=&quot;scrollview-header&quot;&gt;
        &lt;h1&gt;Paged ScrollView&lt;&#x2F;h1&gt;
    &lt;&#x2F;div&gt;

    &lt;div id=&quot;scrollview-content&quot; class=&quot;yui3-scrollview-loading&quot;&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;img src=&quot;http:&#x2F;&#x2F;farm5.static.flickr.com&#x2F;4136&#x2F;4802088086_c621e0b501.jpg&quot; alt=&quot;Above The City II&quot;&gt;&lt;&#x2F;li&gt;
            &lt;li&gt;&lt;img src=&quot;http:&#x2F;&#x2F;farm5.static.flickr.com&#x2F;4114&#x2F;4801461321_1373a0ef89.jpg&quot; alt=&quot;Walls and Canyon&quot;&gt;&lt;&#x2F;li&gt;
            &lt;li&gt;&lt;img src=&quot;http:&#x2F;&#x2F;farm5.static.flickr.com&#x2F;4100&#x2F;4801614015_4303e8eaea.jpg&quot; alt=&quot;Stairs Using In Situ Stone&quot;&gt;&lt;&#x2F;li&gt;
            &lt;li&gt;&lt;img src=&quot;http:&#x2F;&#x2F;farm5.static.flickr.com&#x2F;4076&#x2F;4801368583_854e8c0ef3.jpg&quot; alt=&quot;Tree Silhouette&quot;&gt;&lt;&#x2F;li&gt;
        &lt;&#x2F;ul&gt;
    &lt;&#x2F;div&gt;

    &lt;div id=&quot;scrollview-pager&quot;&gt;
        &lt;button id=&quot;scrollview-prev&quot;&gt;Prev&lt;&#x2F;button&gt;
        &lt;button id=&quot;scrollview-next&quot;&gt;Next&lt;&#x2F;button&gt;
    &lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;

&lt;div id=&quot;additional-content&quot;&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras aliquam hendrerit elit id vulputate. Pellentesque pellentesque erat rutrum velit facilisis sodales convallis tellus lacinia. Curabitur gravida mi sit amet nulla suscipit sed congue dolor volutpat. Aenean sem tortor, pretium et euismod in, imperdiet sit amet urna. Ut ante nisi, auctor mattis suscipit a, ullamcorper eget leo. Phasellus sagittis ante at lectus rutrum ut sollicitudin sem malesuada. Duis ultrices sapien et nulla tincidunt malesuada. Mauris ante turpis, dignissim eu tincidunt vitae, placerat quis diam. In augue nisl, cursus at rutrum ut, scelerisque et erat. Suspendisse potenti. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris orci dui, aliquam ut convallis ut, dapibus et erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam erat volutpat. Mauris placerat elit id lectus rhoncus in dignissim justo mollis. Donec nec odio sapien. In iaculis euismod felis non laoreet. Mauris ornare varius neque, et congue erat porta a. Aliquam nec auctor lectus. Etiam ut ipsum a nibh iaculis fringilla.&lt;&#x2F;p&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras aliquam hendrerit elit id vulputate. Pellentesque pellentesque erat rutrum velit facilisis sodales convallis tellus lacinia. Curabitur gravida mi sit amet nulla suscipit sed congue dolor volutpat. Aenean sem tortor, pretium et euismod in, imperdiet sit amet urna. Ut ante nisi, auctor mattis suscipit a, ullamcorper eget leo. Phasellus sagittis ante at lectus rutrum ut sollicitudin sem malesuada. Duis ultrices sapien et nulla tincidunt malesuada. Mauris ante turpis, dignissim eu tincidunt vitae, placerat quis diam. In augue nisl, cursus at rutrum ut, scelerisque et erat. Suspendisse potenti. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris orci dui, aliquam ut convallis ut, dapibus et erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam erat volutpat. Mauris placerat elit id lectus rhoncus in dignissim justo mollis. Donec nec odio sapien. In iaculis euismod felis non laoreet. Mauris ornare varius neque, et congue erat porta a. Aliquam nec auctor lectus. Etiam ut ipsum a nibh iaculis fringilla.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;

&lt;script type=&quot;text&#x2F;javascript&quot; charset=&quot;utf-8&quot;&gt;
YUI().use(&#x27;scrollview&#x27;, &#x27;scrollview-paginator&#x27;, function(Y) {

    var scrollView = new Y.ScrollView({
        id: &quot;scrollview&quot;,
        srcNode : &#x27;#scrollview-content&#x27;,
        width : 320,
        flick: {
            minDistance:10,
            minVelocity:0.3,
            axis: &quot;x&quot;
        }
    });

    scrollView.plug(Y.Plugin.ScrollViewPaginator, {
        selector: &#x27;li&#x27;
    });

    scrollView.render();

    var content = scrollView.get(&quot;contentBox&quot;);

    content.delegate(&quot;click&quot;, function(e) {
        &#x2F;&#x2F; For mouse based devices, we need to make sure the click isn&#x27;t fired
        &#x2F;&#x2F; at the end of a drag&#x2F;flick. We use 2 as an arbitrary threshold.
        if (Math.abs(scrollView.lastScrolledAmt) &lt; 2) {
            alert(e.currentTarget.getAttribute(&quot;alt&quot;));
        }
    }, &quot;img&quot;);

    &#x2F;&#x2F; Prevent default image drag behavior
    content.delegate(&quot;mousedown&quot;, function(e) {
        e.preventDefault();
    }, &quot;img&quot;);

    Y.one(&#x27;#scrollview-next&#x27;).on(&#x27;click&#x27;, Y.bind(scrollView.pages.next, scrollView.pages));
    Y.one(&#x27;#scrollview-prev&#x27;).on(&#x27;click&#x27;, Y.bind(scrollView.pages.prev, scrollView.pages));

});
&lt;&#x2F;script&gt;</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="This example creates a basic ScrollView which doesn&#x27;t include a scrollbar indicator.">
                                        <a href="scrollview-base.html">Basic ScrollView Without a Scroll Indicator</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example shows the classic Scrollview implementation, including scroll indicators (bars) and including code to suppress link navigation while scrolling.">
                                        <a href="scrollview.html">ScrollView with Scroll Indicator and Link Suppression Behavior</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example creates a horizontal ScrollView.">
                                        <a href="scrollview-horiz.html">Horizontal ScrollView</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example creates a horizontal ScrollView with pagination support.">
                                        <a href="scrollview-paging.html">ScrollView With Pagination</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
